import http from '@/util/http'

const module = {
  namespaced: true, // 命名空间
  state: {
    banners: [],
    gridsV2: [],
    topList: [],
    note: '',
    cartHomeList: [],
    nextIndex: 0,
    categoriesTitle: '',
    categories: []
  },
  actions: {
    getHomeAction (store, tabid) {
      tabid = tabid || 1
      console.log('tabid', tabid)
      http.request({
        url: `/api/tab/${tabid}?start=0`
      }).then(res => {
        if (tabid === 1) {
          store.commit('setBannersList', res.data.data.banners) // 支持传参
          store.commit('setgridsV2', res.data.data.gridsV2)
          store.commit('settopList', res.data.data.topList)
        }
        store.commit('setNote', res.data.data.note)
        store.commit('setCartHomeList', res.data.data.items.list)
        store.commit('setNextIndex', res.data.data.items.nextIndex)
        store.commit('setCategoriesTitle', res.data.data.categoriesTitle)
        store.commit('setCategories', res.data.data.categories)
      })
    },
    getMoreAction (store, params) {
      if (params.path === undefined || params.path === '/') {
        params.path = '/tab/1'
      }
      http.request({
        url: `/api${params.path}/feeds?start=${params.start}&sort=0`
      }).then(res => {
        store.commit('pushCartHomeList', res.data.data.list)
        store.commit('setNextIndex', res.data.data.nextIndex)
      })
    }
  },
  mutations: {
    setCategories (state, data) {
      console.log('categories', data)
      state.categories = data
    },
    setCategoriesTitle (state, data) {
      console.log('categoriesTitle', data)
      state.categoriesTitle = data
    },
    setBannersList (state, data) {
      console.log('banners', data)
      state.banners = data
    },
    setgridsV2 (state, data) {
      console.log('gridsV2', data)
      state.gridsV2 = data
    },
    settopList (state, data) {
      console.log('settopList', data)
      state.topList = data
    },
    setNote (state, data) {
      console.log('note', data)
      state.note = data
    },
    setCartHomeList (state, data) {
      console.log('setCartHomeList', data)
      state.cartHomeList = data
    },
    setNextIndex (state, data) {
      console.log('nextIndex', data)
      state.nextIndex = data
    },
    pushCartHomeList (state, data) {
      console.log('pushCartHomeList', data)
      state.cartHomeList.push(...data)
      console.log(state.cartHomeList.length)
    }
  },
  getters: {
  }
}

export default module
